package com.boho.ecp.util;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class JPEGUtil {

	/**
	 * 根据原图生成缩略图
	 * 
	 * @param srcImg
	 *            原图路径
	 * @param destImg
	 *            缩略图路径
	 */
	public static void createSmallImage(String srcImg, String destImg) {
		try {
			File file = new File(srcImg);
			Image src = ImageIO.read(file);
			int width = src.getWidth(null);
			int height = src.getHeight(null);

			BufferedImage tagImage = new BufferedImage(width / 2, height / 2, BufferedImage.TYPE_BYTE_GRAY);
			Graphics2D g2d = tagImage.createGraphics();
			g2d.rotate(0.5d); // 进行图片旋转处理
			g2d.drawImage(src, 0, 0, width / 2, height / 2, null);
			g2d.drawString("缩略图", width / 2 - 50, height / 2 - 15);
			FileOutputStream output = new FileOutputStream(destImg);
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(output);
			encoder.encode(tagImage);
			output.flush();
			output.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (ImageFormatException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 进行图片切割
	 * 
	 * @param srcFileName
	 *            原始图片
	 * @param descFileName
	 *            目标图片
	 * @param descWidth
	 *            目标宽度
	 * @param descHeight
	 *            目标高度
	 */
	public static void cutImage(String srcFileName, String descFileName,
			int descWidth, int descHeight) {
		File srcFile = new File(srcFileName);
		File descFile = new File(descFileName);
		Image cropImg = null;
		ImageFilter cropFilter = null;

		try {
			BufferedImage bufferImg = ImageIO.read(srcFile);
			int srcWidth = bufferImg.getWidth();
			int srcHeight = bufferImg.getHeight();

			if (srcWidth > descWidth && srcHeight > descHeight) {
				Image img = bufferImg.getScaledInstance(srcWidth, srcHeight,
						Image.SCALE_SMOOTH);
				// descWidth = 200;
				// descHeight = 150;
				int cols = 0; // 切片横向数量
				int rows = 0; // 切片纵向数量
				// 计算切片的横向和纵向数量
				if (srcWidth % descWidth == 0) {
					cols = srcWidth / descWidth;
				} else {
					cols = (int) Math.floor(srcWidth / descWidth) + 1;
				}

				if (srcHeight % descHeight == 0) {
					rows = srcHeight / descHeight;
				} else {
					rows = (int) Math.floor(srcHeight / descHeight) + 1;
				}

				for (int i = 0; i < rows; i++) {
					for (int j = 0; j < cols; j++) {
						cropFilter = new CropImageFilter(200 * j, 150 * i,
								descWidth, descHeight);
						cropImg = Toolkit.getDefaultToolkit().createImage(
								new FilteredImageSource(img.getSource(),
										cropFilter));

						BufferedImage tag = new BufferedImage(descWidth,
								descHeight, BufferedImage.TYPE_INT_RGB);
						Graphics g = tag.getGraphics();
						g.drawImage(img, 0, 0, null);
						g.dispose();
						ImageIO.write(tag, "JPEG", descFile);
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 给图片添加图片水印
	 * 
	 * @param pressImg
	 *            水印图片
	 * @param targetImg
	 *            目标图片
	 * @param x
	 *            修正值,默认在中间
	 * @param y
	 *            修正值,默认在中间
	 * @param alpha
	 *            透明度
	 */
	public static void pressImage(String pressImg, String targetImg, int x,
			int y, float alpha) {
		File pressFile = new File(pressImg);
		File targetFile = new File(targetImg);

		try {
			Image src = ImageIO.read(targetFile);
			int srcWidth = src.getWidth(null);
			int srcHeight = src.getHeight(null);
			BufferedImage image = new BufferedImage(srcWidth, srcHeight,
					BufferedImage.TYPE_INT_BGR);
			Graphics2D g2d = image.createGraphics();
			g2d.drawImage(src, 0, 0, srcWidth, srcHeight, null);

			Image dest = ImageIO.read(pressFile);
			int destWidth = dest.getWidth(null);
			int destHeight = dest.getHeight(null);
			g2d.setComposite(AlphaComposite.getInstance(
					AlphaComposite.SRC_ATOP, alpha));
			g2d.drawImage(dest, (srcWidth - destWidth) / 2,
					(srcHeight - destHeight) / 2, destWidth, destHeight, null);
			g2d.dispose();
			ImageIO.write(image, "JPG", targetFile);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 给图片添加文字水印
	 * 
	 * @param pressText
	 *            水印文字
	 * @param targetImg
	 *            目标图片
	 * @param fontName
	 *            字体名称
	 * @param fontStyle
	 *            字体样式
	 * @param color
	 *            字体颜色
	 * @param fontSize
	 *            字体大小
	 * @param x
	 *            修正值
	 * @param y
	 *            修正值
	 * @param alpha
	 *            透明度
	 */
	public static void pressText(String pressText, String targetImg,
			String fontName, int fontStyle, Color color, int fontSize, int x,
			int y, float alpha) {
		File targetFile = new File(targetImg);

		try {
			Image src = ImageIO.read(targetFile);
			int width = src.getWidth(null);
			int height = src.getHeight(null);
			BufferedImage image = new BufferedImage(width, height,
					BufferedImage.TYPE_INT_RGB);
			Graphics2D g2d = image.createGraphics();
			g2d.drawImage(src, 0, 0, width, height, null);
			g2d.setColor(color);
			g2d.setFont(new Font(fontName, fontStyle, fontSize));
			g2d.setComposite(AlphaComposite.getInstance(
					AlphaComposite.SRC_ATOP, alpha));
			g2d.drawString(pressText, (width - getLength(pressText) * fontSize)
					/ 2 + x, (height - fontSize) / 2 + y);
			g2d.dispose();
			ImageIO.write(image, "JPG", targetFile);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获得字符实际字节数
	 * 
	 * @param text
	 * @return
	 */
	public static int getLength(String text) {
		int length = 0;
		for (int i = 0; i < text.length(); i++) {
			if (new String(text.charAt(i) + "").getBytes().length > 1) {
				length += 2;
			} else {
				length += 1;
			}
		}
		return length / 2;
	}

	public static void main(String[] args) {
//		JPEGUtil.createSmallImage("D:/100010.jpg", "D:/test.jpg");
//		 JPEGUtil.cutImage("D:/100010.jpg", "D:/cut.jpg", 400, 400);
		 JPEGUtil.pressText("可爱的鞋子", "D:/100010.jpg", "楷体", Font.BOLD, Color.PINK, 14, 5, 5, 0.7f);
	}
}
